Python Tuple

    In this tutorial, we are going to discuss Python tuple. Tuples are most likely python lists so it's very important to have knowledge of list before you learn python tuples. Here in this article, we will learn all about python tuples and all methods & logics associated with it. A tuple is a data type present in python which acts and mostly look like a python list. Unlike a list, we use parenthesis () to store tuple items. Tuples are immutable in nature which means once items have assigned to a tuple cannot be altered. As we have mentioned that list and tuples can perform the same task, like a list we can store different data types in a tuple.

    Let’s understand it with an example:

    >>> tup = (20,"Hello","world",19,True)
    >>> print(tup)
    >>> print(type(tup))

    #output

     (20, 'Hello', 'world', 19, True)
    <class 'tuple'>

    Tuple inside a tuple

    tup = (2019, "Jan", ("hello", "world") , "Monday" )
    print(tup)

    #Output

    (2019, 'Jan', ('hello', 'world'), 'Monday')

    Indexing in a tuple:

    Like a list, in tuple, we also use indexing to access the specific item present inside it. As we know that by convention indexing start from 0 and 0 index represent the 1 st item of the tuple. 3 type of indexing we can perform on tuples:

    1. Positive indexing

    In positive indexing, we use the positive integers (start from 0 to n-1 integers for n items in the tuple) to retrieve the specific item from a tuple.

    Let’s understand it with an example:

    tup = ("a","b","c","d","e","f",("g","h"),"i")
    print(tup[0])
    print(tup[3])
    print(tup[5])
    print(tup[6])
    print(tup[6][0])                 #tuple inside a tuple

    #Output

    a
    d
    f
    ('g', 'h')
    g

    2. Negative indexing:

    In negative we pass negative integer in [ ]  sq. box to retrieve the specific item from a tuple. Here 0 also represent the 1 st item of the tuple but the -1 represent the last item of tuple. Negative indexing comes in use when we want to retrieve items from the back of tuple.

    Let’s understand it with an example:

    tup = ("a","b","c","d","e","f")
    print(tup[0])
    print(tup[-2])
    print(tup[-5])

    #Output

    a
    e
    b

    3 Slicing:

    By using the slicing index, we can access a sequence of a tuple. Syntax of using a slicing index. tup[start point : end point : steps]

    Let’s understand it with an example:

    tup = ("a","b","c","d","e","f",("g","h"),"i")
    print(tup[0:3])
    print(tup[3:-1:])
    print(tup[6][0:-1])

    #Output

    ('a', 'b', 'c')
    ('d', 'e', 'f', ('g', 'h'))
    ('g', 'h')

    Changing a tuple:

    Tuples are immutable that means once we have an assigned a value to a tuple, we cannot change it.

    Let’s understand it with an example:

    tup = ("a","b","c","d","e","f",("g","h"),"i")
    tup [3] = "k"

    #Output

    TypeError: 'tuple' object does not support item assignment

    But we can change a mutable item if it presents in a tuple though we can not change all the mutable item for once.

    Let’s understand it with an example

    tup = ("a","b","c","d","e","f",["g","h"])
    tup [6] = "k"

    #Output

    TypeError: 'tuple' object does not support item assignment

    tup = ("a","b","c","d","e","f",["g","h"])
    tup [6][0] = "k"
    print(tup)

    #Output

    ('a', 'b', 'c', 'd', 'e', 'f', ['k', 'h'])

    Deleting a tuple:

    We can use del keyword to delete the tuple object Example

    tup = ("a","b","c","d","e","f",["g","h"])
    del tup
    print(tup)

    #Output

    NameError: name 'tup' is not defined

    Convert a list to a tuple

    We can convert a list into a tuple using tuple keyword.

    Example:

    lis = ["a","b","c","d","e","f",["g","h"]]
    print(type (lis))
    tup =tuple(lis)
    print(type(tup))

    #Output

    <class 'list'>
    <class 'tuple'>

    Tuple methods:

    Unlike list tuple does not many methods, tuple only has two methods.

    • count(a): It uses to count the number of items a present in tuple
    • index(x): it return the index value of x item

    Example:

    tup =(1,2,3,3,4,5,5,5,6,5,4,3,5)
    print(tup.count(5))
    print(tup.index(3))

    #Output

    5
    2

    Advantages of Tuple over List:

    • Conventionally we use the list for heterogeneous data types and tuple for homogenous data type
    • Tuples are faster than list
    • Tuples can be used as a key of a dictionary
    • Data items of a tuple cannot be manipulated.